/**
 * 
 */
package pl.com.hme.mediaplayer.player;

import java.io.IOException;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.media.MediaPlayer.OnVideoSizeChangedListener;
import android.util.Log;
import android.view.SurfaceHolder;

/**
 * @author DB
 * 
 */
public class GenericMediaPlayer implements OnPreparedListener, OnCompletionListener, OnBufferingUpdateListener {

    private static final String TAG = "GenericMediaPlayer";
    private MediaPlayer mMediaPlayer;
    private GenericOnPreparedListener mOnPreparedListener;
    private GenericOnCompletionListener mOnCompletionListener;
    private GenericOnBufferingUpdateListener mOnBufferingUpdateListener;
    private OnVideoSizeChangedListener mOnVideoSizeChangedListener;
    private int mPosition;
    private boolean mIsInit = false;
    private boolean mIsStarting = false;

    private SurfaceHolder mSurfaceHolder;

    public void createPlayer() {
        Log.d(TAG, "CreatePlayer");
        mMediaPlayer = new MediaPlayer();
        mMediaPlayer.setOnPreparedListener(this);
        mMediaPlayer.setOnCompletionListener(this);
        mMediaPlayer.setOnBufferingUpdateListener(this);
        if (mOnVideoSizeChangedListener != null) {
            mMediaPlayer.setOnVideoSizeChangedListener(mOnVideoSizeChangedListener);
        }
    }

    public void releasePlayer() {
        Log.d(TAG, "ReleasePlayer");
        if (mMediaPlayer != null) {
            mMediaPlayer.reset();
            mMediaPlayer.release();
        }
        mMediaPlayer = null;
        mIsInit = false;
        mIsStarting = false;
    }

    public void startPlayer(SurfaceHolder sh, String link, int pos) throws IllegalArgumentException, IOException {
        Log.d(TAG, "StartPlayer");
        if (mIsStarting == false) {
            mSurfaceHolder = sh;
            if (mMediaPlayer == null) {
                createPlayer();
            }
            mIsStarting = true;

            mPosition = pos;
            Log.d(TAG, "SetDataSource: " + link);
            mMediaPlayer.setDataSource(link);
            mMediaPlayer.setDisplay(mSurfaceHolder);
            mMediaPlayer.setScreenOnWhilePlaying(true);
            mMediaPlayer.prepareAsync();
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

        }
    }

    public boolean start() {
        Log.d(TAG, "Start");
        if (mMediaPlayer != null && isInit()) {
            mMediaPlayer.start();
            return true;
        } else {
            return false;
        }
    }

    public void prepareAsync() {
        if (mMediaPlayer != null) {
            mMediaPlayer.prepareAsync();
        }
    }

    public boolean pause() {
        Log.d(TAG, "Pause");
        if (mMediaPlayer != null && isPlaying()) {
            mMediaPlayer.pause();
            return true;
        } else {
            return false;
        }
    }

    public int getCurrentPosition() {
        if (mMediaPlayer != null) {
            return mMediaPlayer.getCurrentPosition();
        } else {
            return -1;
        }
    }

    public boolean isPlaying() {
        if (mMediaPlayer != null)
            return mMediaPlayer.isPlaying();
        else return false;
    }

    public boolean isInit() {
        return mIsInit;
    }

    public void setOnPreparedListener(GenericOnPreparedListener listener) {
        mOnPreparedListener = listener;
    }

    public void setOnCompletionListener(GenericOnCompletionListener listener) {
        mOnCompletionListener = listener;
    }

    public void setOnBufferingUpdateListener(GenericOnBufferingUpdateListener listener) {
        mOnBufferingUpdateListener = listener;
    }

    @Override
    public void onPrepared(MediaPlayer mp) {
        Log.d(TAG, "OnPrepared");
        mIsInit = true;
        mIsStarting = false;
        if (!isPlaying()) {
            Log.d(TAG, "Start");
            mp.start();
            if (mPosition != 0) {
                Log.d(TAG, "SeekTo: " + mPosition);
                mp.seekTo(mPosition);
            }
        }
        if (mOnPreparedListener != null) {
            mOnPreparedListener.onPrepared(this);
        }
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        Log.d(TAG, "OnCompletion");
        releasePlayer();
        if (mOnCompletionListener != null) {
            mOnCompletionListener.onCompletion(this);
        }
    }

    @Override
    public void onBufferingUpdate(MediaPlayer mp, int percent) {
        // Log.d(TAG, "OnBufferingUpdate: " + percent);
        if (mOnBufferingUpdateListener != null) {
            mOnBufferingUpdateListener.onBufferingUpdate(this, percent);
        }
    }

    public void setOnVideoSizeChangedListener(OnVideoSizeChangedListener onVideoSizeChangedListener) {
        mOnVideoSizeChangedListener = onVideoSizeChangedListener;
        if (mMediaPlayer != null) {
            mMediaPlayer.setOnVideoSizeChangedListener(mOnVideoSizeChangedListener);
        }
    }

    public int getDuration() {
        if (mMediaPlayer != null) {
            return mMediaPlayer.getDuration();
        } else {
            return 0;
        }

    }

}
